package com.gz.mylife.im;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;

import com.gz.mylife.Define;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.zl.smartmall.library.account.IAuth;
import com.zl.smartmall.library.concurrent.ThreadExecutor;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;
import com.zl.smartmall.library.util.PreferenceModel;
import com.zl.smartmall.library.util.SingtonAsyncHttpClient;

public class UpLoaderHidePmid {

	private static UpLoaderHidePmid instance;
	
	public UpLoaderHidePmid(){
		
	}
	public static UpLoaderHidePmid getInstance(Context context){
		if(instance == null){
			synchronized (UpLoaderHidePmid.class) {
				if(instance == null){
					synchronized (UpLoaderHidePmid.class) {
						instance = new UpLoaderHidePmid();
					}
				}
			}
			return instance;
		}else{
			return instance;
		}
	}
	
	public  void upLoad(final int pmid){
		ThreadExecutor.getInstance().execute(new Runnable() {
			
			@Override
			public void run() {
				synchronized (UpLoaderHidePmid.class) {
					String pStr = PreferenceModel.getString(Define.KEY_UPLOAD_HIDE_PIM, "");
					if(!pStr.contains(""+pmid)){
						pStr = TextUtils.isEmpty(pStr)?pmid+"":pStr+","+pmid;
						PreferenceModel.putString(Define.KEY_UPLOAD_HIDE_PIM, pStr);
						CrashHandler.d("uploadpmid", "before:"+pStr);
					}
				}
				String url = Constants.URL_HIDE_PMID;
				SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
				url = String.format(url+"?pmid=%s", pmid+"");
				SingtonAsyncHttpClient.getInstance().get(url, new HttpHandler(pmid));
			}
		});
	}
	public void upLoadAll(){
			String pStr = PreferenceModel.getString(Define.KEY_UPLOAD_HIDE_PIM, "");
			if(TextUtils.isEmpty(pStr)){
				return;
			}
			String pmids[] = pStr.split(",");
			CrashHandler.d("uploadpmid","uploadall:"+pStr);
			if(pmids != null && pmids.length>0){
				for(int i = 0;i<pmids.length;i++){
					try {
						final int pmid = Integer.parseInt(pmids[i]);
						ThreadExecutor.getInstance().execute(new Runnable() {
							
							@Override
							public void run() {
								String url = Constants.URL_HIDE_PMID;
								SingtonAsyncHttpClient.getInstance().setCookieStore(IAuth.getInstance().loadCookieStore());
								url = String.format(url+"?pmid=%s", pmid+"");
								SingtonAsyncHttpClient.getInstance().get(url, new HttpHandler(pmid));
							}
						});
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		
		
	}
	
	public class HttpHandler extends JsonHttpResponseHandler{
			private int pmid;
			public HttpHandler(int pmid){
				this.pmid = pmid;
			}
			@Override
			public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
				try {
					int ret = response.getInt("ret");
					if(ret == 0){
						synchronized (UpLoaderHidePmid.class) {
							String pStr = PreferenceModel.getString(Define.KEY_UPLOAD_HIDE_PIM, "");
							if(pStr.contains(","+pmid)){
								pStr = pStr.replace(","+pmid, ""); 
							}else if(pStr.startsWith(pmid+",")){
								pStr = pStr.replace(pmid+",", "");
							}else if(pStr.startsWith(""+pmid)){
								pStr = pStr.replace(""+pmid, ""); 
							}
							PreferenceModel.putString(Define.KEY_UPLOAD_HIDE_PIM, pStr);
							CrashHandler.d("uploadpmid", "success:"+pStr);
						}
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, JSONObject errorResponse) {
				super.onFailure(statusCode, headers, throwable, errorResponse);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
			}
	}
}
